var nhwRoom = function(matchServer,app,uidArr,roomId) {
	this.app = app
	this.channelService = this.app.get('channelService')
	this.matchServer = matchServer
	this.uidArr = uidArr
	this.createTime = Date.now()
	this.roomData = {}
	this.userState = {}
	this.roomId = roomId
	this.question_bank = require("../cfg/question_nhw.json")
	for(var i = 0;i < this.uidArr.length;i++){
		this.userState[this.uidArr[i]] = {online : true}
	}
	//房间数据
	this.state = 0				//房间状态  0 看题阶段  1  答题接口
	this.question_list = []
	this.question_num = 0		//当前题目数量
	this.question_max = 5		//最大题目数量
	this.question_cur = {}		//当前题目
	this.user_answer = {}		//玩家答案
	this.user_score = {} 		//玩家分数
	for(var i = 0;i < this.uidArr.length;i++){
		this.user_score[this.uidArr[i]] = 0
	}
	this.firstFlag = true 		//先手标识
	this.question_time = 0		//题目下发时间戳
	this.beginTime = 1000 	//开始时间
	this.topicTime = 3000 	//看题时间
	this.answerTime = 10000	//答题时间
	this.showTime = 2000		//显示答案时间
	this.timer = 0
	// console.log("南海网专用")
}
nhwRoom.prototype.init = function() {
	let notify = {
		type : "matchSuccess",
		roomInfo : {
			uidArr : this.uidArr,
			roomData : this.roomData,
			createTime : this.createTime
		}
	}
	this.sendToRoom(notify)
	this.begin()
}
//答题开始
nhwRoom.prototype.begin = function() {
	// console.log("答题开始")
	for(var i in this.question_bank){
		this.question_list.push(i)
	}
	var notify = {
		type : "begin",
		time : this.beginTime
	}
	this.sendToRoom(notify)
	clearTimeout(this.timer)
	this.timer = setTimeout(this.topic.bind(this),this.beginTime)
}
//下发题目
nhwRoom.prototype.topic = function() {
	this.question_num++
	var index = Math.floor(Math.random() * this.question_list.length)
	this.question_cur = this.question_bank[this.question_list[index]]
	this.question_list.splice(index,1)
	var notify = {
		type : "topic",
		question_num : this.question_num,
		question : this.question_cur
	}
	this.sendToRoom(notify)
	clearTimeout(this.timer)
	this.timer = setTimeout(this.question.bind(this),this.topicTime)
	// console.log("下发题目",this.question_cur)
}
//答题阶段
nhwRoom.prototype.question = function() {
	this.firstFlag = true
	this.user_answer = {}
	this.question_time = Date.now()
	this.state = 1
	var notify = {
		type : "question",
		question_num : this.question_num,
		question : this.question_cur
	}
	this.sendToRoom(notify)
	clearTimeout(this.timer)
	this.timer = setTimeout(this.showAnswer.bind(this),this.answerTime)
	// console.log("答题阶段")
}
//提交答案
nhwRoom.prototype.submitAnswer = function(uid,msg,cb) {
	if(!this.state){
		cb(false,"不在答题阶段")
		return
	}
	var answer = msg.answer
	if(this.user_answer[uid]){
		cb(false,"已提交答案")
		return
	}
	let useTime = Math.ceil((Date.now() - this.question_time) / 1000)
	var notify = {
		type : "submitAnswer",
		add : 0,
		score : this.user_score[uid],
		useTime : useTime,
		firstFlag : false,
		uid : uid,
		answer : answer,
		right : false
	}
	this.user_answer[uid] = answer
	if(answer === this.question_cur.answer){
		var score = useTime * 10
		if(this.question_num >= this.question_max)
			score = Math.floor(score* 2)
		//答对
		if(this.firstFlag){
			score += 20
			this.firstFlag = false
			notify.firstFlag = false
		}
		this.user_score[uid] += score
		notify.socre = this.user_score[uid]
		notify.add = score
		notify.right = true
	}
	this.sendToRoom(notify)
	// console.log("提交答案",notify)
	//判断提前结束
	var flag = true
	for(var i in this.user_score){
		if(!this.user_answer[i]){
			flag = false
			break
		}
	}
	if(flag){
		clearTimeout(this.timer)
		this.showAnswer()
	}
	cb(true)
}
nhwRoom.prototype.getRoomInfo = function(uid,msg,cb) {
	var roomInfo = {
		state : this.state,
		question_num : this.question_num,
		question_max : this.question_max,
		question_cur : this.question_cur,
		user_answer : this.user_answer,
		user_score : this.user_score,
		firstFlag : this.firstFlag,
		question_time : this.question_time,
		beginTime : this.beginTime,
		answerTime : this.answerTime,
		showTime : this.showTime,
		topicTime : this.topicTime
	}
	cb(true,roomInfo)
}
//公布答案
nhwRoom.prototype.showAnswer = function() {
	this.state = 0
	var notify = {
		type : "showAnswer",
		user_score : this.user_score,
		user_answer : this.user_answer,
		question : this.question_cur
	}
	this.sendToRoom(notify)
	// console.log("公布答案",notify)
	if(this.question_num >= this.question_max){
		this.gameOver()
	}else{
		clearTimeout(this.timer)
		setTimeout(this.topic.bind(this),this.showTime)
	}
}
//答题结束
nhwRoom.prototype.gameOver = function() {
	// console.log("答题结束")
	this.matchServer.finishRoom(this.roomId)
}
module.exports = {
	id : "nhwRoom",
	parent: "defaultRoom",
	func : nhwRoom,
	scope : "prototype",
	init : "init",
	args : [{
		name : "matchServer",
		type : "Object"
	},{
		name : "app",
		type : "Object"
	},{
		name : "uidArr",
		type : "Object"
	},{
		name : "roomId",
		type : "String"
	}]
}